'use strict';

const Logic = require('thinkjs').think.Logic;

class Account extends Logic {

    /**
     * 验证购买会员数据
     */
    async membershipAction() {
        const { ctx } = this;
        let rules;
        if (ctx.isAjax()) {
            rules = {
                groupId: { int: true, required: true },
                type: { string: true, required: true },
                time: { int: true, required: false, length: { min: 1, max: 100 } },
                price: { currency: true, required: true },
                step: { int: true, required: true },
            };
            const flag = this.validate(rules);

            if (!flag) {
                return this.fail('validate error', this.validateErrors);
                // 如果校验失败，返回
                // {"errno":1000,"errmsg":"validate error","data":{"username":"username can not be blank"}}
            }
        }
    }

    /**
     * 验证购买会员数据
     */
    async renewAction() {
        const { ctx } = this;
        let rules;
        if (ctx.isAjax()) {
            rules = {
                type: { string: true, required: true },
                time: { int: true, required: false, length: { min: 1, max: 100 } },
                price: { currency: true, required: true },
                step: { int: true, required: true },
            };
            const flag = this.validate(rules);

            if (!flag) {
                return this.fail('validate error', this.validateErrors);
                // 如果校验失败，返回
                // {"errno":1000,"errmsg":"validate error","data":{"username":"username can not be blank"}}
            }
        }
    }

    /**
     * 验证升级用户组数据
     */
    async upgradeAction() {
        const { ctx } = this;
        let rules;
        if (ctx.isAjax()) {
            rules = {
                groupId: { int: true, required: true },
                price: { currency: true, required: true },
                step: { int: true, required: true },
            };
            const flag = this.validate(rules);

            if (!flag) {
                return this.fail('validate error', this.validateErrors);
                // 如果校验失败，返回
                // {"errno":1000,"errmsg":"validate error","data":{"username":"username can not be blank"}}
            }
        }
    }
}

module.exports = Account;
